package com.youlu.campus.service.visitor.impl;

import com.youlu.campus.common.utils.TimeUtils;
import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.UserPointDailySignIn;
import com.youlu.campus.entity.UserPointsDetail;
import com.youlu.campus.service.platform.PointPlatformConfigService;
import com.youlu.campus.service.visitor.UserPointDailySignInService;
import com.youlu.campus.service.visitor.UserPointsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Service
@Slf4j
public class UserPointDailySignInServiceImpl implements UserPointDailySignInService {
    @Autowired
    private MongoTemplate mongoTemplatel;
    @Lazy
    @Autowired
    private UserPointsService userPointsService;
    @Autowired
    private PointPlatformConfigService pointPlatformConfigService;

    @Override
    public boolean signIn(UserPO currentUser,String appId, Date date) {
        log.info(":>>> 开始签到:{},data:{}",currentUser.getId(), date);
        if (StringUtils.isBlank(currentUser.getId()) || Objects.isNull(date)) {
            log.error(":>>> 签到参数错误:用户ID或日期为空");
            return false;
        }
        Date dayTime = TimeUtils.getDayStartTime(date);
        Query query = new Query().addCriteria(Criteria.where("dayTime").is(dayTime).and("userId").is(currentUser.getId()));
        boolean exist = mongoTemplatel.exists(query, UserPointDailySignIn.class);
        if (exist) {
            log.error(":>>> 用户已经签到,请不要重复签到");
            return true;
        }
        Integer point = pointPlatformConfigService.getPoint(appId,"sign-up");
        Date current = new Date();
        UserPointDailySignIn insert = new UserPointDailySignIn();
        insert.setUserId(currentUser.getId());
        insert.setDayTime(dayTime);
        insert.setPoint(point);
        insert.setCreatedTime(current);
        insert.setSignInTime(current);
        insert.setId(null);
        mongoTemplatel.insert(insert);
        UserPointsDetail detail = new UserPointsDetail();
        detail.setUserId(currentUser.getId());
        detail.setAction("add");
        detail.setBizDesc("每日签到");
        detail.setBizType("sign-up");
        detail.setOptDate(current);
        detail.setPoints(point);
        detail.setPlatform(currentUser.getCurrentPlatform());
        detail.setAppId(currentUser.getAppId());
        detail.setComments("每日签到获得" + point + "积分");
        userPointsService.addPoint(detail);
        return true;
    }

    @Override
    public boolean signIned(String userId, Date date) {
        Date dayTime = TimeUtils.getDayStartTime(date);
//        Query query = new Query().addCriteria(Criteria.where("dayTime").is(dayTime).and("userId").is(userId));
        Query query = new Query().addCriteria(Criteria.where("userId").is(userId).and("dayTime").is(dayTime));
        boolean exist = mongoTemplatel.exists(query, UserPointDailySignIn.class);
        log.info(":>>> 用户:{},日期:{} 积分签到结果为:{}", userId, date, exist);
        if (exist) {
            log.error(":>>> 用户:{} 已经签到", userId);
            return true;
        }
        log.info(":>>> 用户:{} 未见到签到", userId);
        return false;
    }
}
